/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sun.ShipperProcessor;

import com.sun.Entity.Productinfo;
import com.sun.Entity.Shippingorder;
import com.sun.Facade.ProductinfoFacadeLocal;
import com.sun.Facade.ShippingorderFacadeLocal;
import exceptions.OrderAlreadyExistException;
import exceptions.ProductNotSupportedException;
import exceptions.QuantityNegativeException;
import javax.ejb.EJB;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.ejb.Stateless;

/**
 *
 * @author test
 */
@WebService()
@Stateless()
public class CreateShippingOrderWS {

    @EJB
    private ProductinfoFacadeLocal productinfoFacade;
    @EJB
    private ShippingorderFacadeLocal shippingorderFacade;

    /**
     * Web service operation
     */
    @WebMethod(operationName = "CreateShippingOrder")
    public void CreateShippingOrder(
            @WebParam(name = "OrderID") String OrderID,
            @WebParam(name = "ProductTypeID") String ProductTypeID,
            @WebParam(name = "Quantity") int Quantity)
            throws
            OrderAlreadyExistException,
            QuantityNegativeException,
            //UnknownProductIdException,
            ProductNotSupportedException {

        // Verification if OrderID not already exist
        Shippingorder order = shippingorderFacade.find(OrderID);
        if (order != null) {
            throw new OrderAlreadyExistException();
        }

        // Verification produt shipping condition exist in database (annuaire)
        Productinfo prod = productinfoFacade.find(ProductTypeID);
        if (prod == null) {
            //throw new UnknownProductIdException();
            throw new ProductNotSupportedException();
        }
        if (prod.getShippingconditions().equals("XL")) {
            throw new ProductNotSupportedException();
        }

        // Verification If quantity is a valid value (> 0)
        if (Quantity <= 0) throw new QuantityNegativeException();

        // Create a order line
        order = new Shippingorder();
        order.setSorderid(OrderID);
        order.setProducttypeid(ProductTypeID);
        order.setProductquantity(Quantity);

        shippingorderFacade.create(order);
    }
}
